﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ITunesSolver.UI
{
    /// <summary>
    /// Interaction logic for FileLoad.xaml
    /// </summary>
    public partial class LoadFilesForm : Window
    {
        public delegate void FileASelectedDelegate(object sender, string fileName);
        public event FileASelectedDelegate FileASelectedEvent;

        public delegate void FileBSelectedDelegate(object sender, string fileName);
        public event FileBSelectedDelegate FileBSelectedEvent;

        public delegate void FilesSelectedDelegate();
        public event FilesSelectedDelegate FilesSelectedEvent;

        public string FileNameA { get; set; }
        public string FileNameB { get; set; }

        public LoadFilesForm()
        {
            InitializeComponent();

            InitializeControls();
        }

        private void InitializeControls()
        {
            sfFileA.FileSelectedEvent += new SelectFile.FileSelectedDelegate(sfFileA_FileSelectedEvent);
            sfFileB.FileSelectedEvent += new SelectFile.FileSelectedDelegate(sfFileB_FileSelectedEvent);

            btnSubmit.IsEnabled = false;
        }

        void sfFileA_FileSelectedEvent(object sender, string fileName)
        {
            FileNameA = fileName;

            btnSubmit.IsEnabled = IsValid;

            if (FileASelectedEvent != null) FileASelectedEvent(sender, fileName);
        }

        void sfFileB_FileSelectedEvent(object sender, string fileName)
        {
            FileNameB = fileName;

            btnSubmit.IsEnabled = IsValid;

            if (FileBSelectedEvent != null) FileBSelectedEvent(sender, fileName);
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnSubmit_Click(object sender, RoutedEventArgs e)
        {
            if (!IsValid) return;

            this.Close();
            if (FilesSelectedEvent != null) FilesSelectedEvent();
        }

        public bool IsValid
        {
            get { 
                bool validA = sfFileA.Validate();
                bool validB = sfFileB.Validate();
                return validA && validB;
            }
        }
    }
}
